package com.yao.club.service.impl;

import com.netease.yidun.sdk.core.utils.StringUtils;
import com.yao.club.entity.result.ResponseEntity;
import com.yao.club.entity.Topic;
import com.yao.club.entity.dto.PostsCollectRecord;
import com.yao.club.entity.dto.PostsLikeRecord;
import com.yao.club.entity.dto.SysImg;
import com.yao.club.entity.result.ResultEntity;
import com.yao.club.entity.vo.HotTopicsVO;
import com.yao.club.entity.vo.SearchSuggestionVO;
import com.yao.club.entity.vo.PostsInfoVO;
import com.yao.club.entity.UserEntity;

import com.yao.club.mapper.PostsInfoMapper;
import com.yao.club.mapper.PostsTopicMapper;
import com.yao.club.mapper.UserMapper;

import com.yao.club.mapper.UserFollowMapper;
import com.yao.club.reposittory.*;
import com.yao.club.service.PostsSearchService;
import com.yao.club.utils.ResponseUtils;
import org.apache.hc.core5.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

import java.util.ArrayList;


@Service
public class PostsSearchServiceImpl implements PostsSearchService {

    @Resource
    private PostsTopicMapper topicMapper;

    
    @Resource
    private UserMapper userMapper;

    @Resource
    private PostsInfoMapper postInfoMapper;

    @Resource
    private SysImageRepository imageRepository;

    @Autowired
    PostsLikeRecordRepository postLikeRepository;
    @Autowired
    PostsCollectRecordRepository postsCollectRepository;
    @Autowired
    PostsShieldRecordRepository postsShieldRepository;

    @Resource
    private UserFollowMapper userFollowMapper;
    
    private static final String TOPIC_SEARCH_CACHE_PREFIX = "topic_search:";
    private static final long CACHE_DURATION = 10; // 缓存10分钟

    @Override
    public ResponseEntity<HotTopicsVO> getHotTopics() {
        try {
            HotTopicsVO response = new HotTopicsVO();
            
            // 获取热门话题（限制返回5个）
            List<Topic> hotTopics = topicMapper.selectTopics(3);
            response.setTopicList(hotTopics);
            
            List<Topic> informationList = topicMapper.selectTopics(4);
            response.setInformationList(informationList);


            return ResponseUtils.success(response);
        } catch (Exception e) {
            return ResponseUtils.failed("获取热门话题失败：" + e.getMessage());
        }
    }

    @Override
    public ResponseEntity<List<SearchSuggestionVO>> searchKeywordList(String keyword) {
        try {
            List<Topic> topics = topicMapper.searchTopics(keyword);
            List<PostsInfoVO> posts = postInfoMapper.searchAllValidPosts(keyword,1,100);

            List<SearchSuggestionVO> suggestions = new ArrayList<>();
            // 添加话题建议
            for (Topic topic : topics) {
                SearchSuggestionVO suggestion = new SearchSuggestionVO();
                suggestion.setSocialType(1); // 话题
                suggestion.setSocialId(topic.getTopicId()+"");
                suggestion.setKeyword(topic.getTopicName());
                suggestions.add(suggestion);
            }
            // 添加帖子建议
            for (PostsInfoVO post : posts) {
                SearchSuggestionVO suggestion = new SearchSuggestionVO();
                suggestion.setSocialType(2); // 帖子
                suggestion.setSocialId(post.getPosts_id()+"");
                suggestion.setKeyword(post.getContent());
                suggestions.add(suggestion);
            }

            return ResponseUtils.success(suggestions);
        } catch (Exception e) {
            return ResponseUtils.failed("获取搜索建议失败：" + e.getMessage());
        }
    }




} 